import {action, Service, util} from "adajs";
import menu from "../../menu.json";

class ContainerService extends Service {
    defaultData() {
        return {
            menu: menu,
            title: "",
            desc: "",
            type: null,
            keywords: "",
            link: "",
            userInfo: window.localStorage.baseInfo ? JSON.parse(window.localStorage.baseInfo).userInfo || {} : {}
        };
    }

    @action("flip")
    flip(current, name) {
        let target = current.menu.find(item => item.link === name);
        if (target) {
            if (!target.type) {
                return import(target.name).then(type => {
                    target.type = type;
                    current.title = target.title;
                    current.desc = target.desc;
                    current.keywords = target.keywords;
                    current.type = type;
                    current.link = name;
                });
            } else {
                current.title = target.title;
                current.desc = target.desc;
                current.keywords = target.keywords;
                current.type = target.type;
                current.link = name;
            }
        }
    }
}

export default ContainerService;